﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace MMOG
{
    public partial class ClientInterface : Form
    {
        public ClientInterface()
        {
            InitializeComponent();
        }

        private void chatBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                Information info = new Information(Information.CHAT_MESSAGE);
                info.add("message", chatBox.Text);
                lock (Client.writer)
                {
                    Client.writer.WriteLine(info.ToString());
                    Client.writer.Flush();
                }
                chatBox.Text = "";
            }
        }

        private void quitButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to close the program?", "Close program",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                Client.exited = true;
        }

        private void chooseButton_Click(object sender, EventArgs e)
        {
            //Client.gameChooser.Show();
            Client.lobbyForm.Show();
            Client.lobbyForm.roomRefreshTimer.Enabled = true;
            Client.lobbyForm.roomRefreshTimer.Start();
        }

        private void inviteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(object);
        }

        private void bugButton_Click(object sender, EventArgs e)
        {
            BugReport reportForm = new BugReport();
            reportForm.ShowDialog();
            if (reportForm.sent)
            {
                Information info = new Information(Information.BUG_REPORT);
                string msg = "";
                for (int i = 0; i < reportForm.textBox.Lines.Length; i++)
                    msg += reportForm.textBox.Lines[i] + " ";
                info.add("message", msg);
                lock (Client.writer)
                {
                    Client.writer.WriteLine(info.ToString());
                    Client.writer.Flush();
                }
            }
        }

        private void optionButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("No option available for this version.");
        }

        private void gameTimer_Tick(object sender, EventArgs e)
        {
            int total = ++Client.gameTime;
            int h = total / 3600;
            int m = (total % 3600) / 60;
            int s = total % 60;
            timeLabel.Text = "" + h + ":" + m + ":" + s;
        }

        private void chatHistory_TextChanged(object sender, EventArgs e)
        {
            //SuspendLayout();
            chatHistory.ScrollToCaret();
            //ResumeLayout(true);
        }

        private void ClientInterface_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to close the program?", "Close program",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
                Client.exited = true;

        }
    }
}
